package com.tzy.dishes.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tzy.common.constant.Result;
import com.tzy.common.constant.WebResult;
import com.tzy.common.constant.WebResultMsgCode;
import com.tzy.common.controller.BaseController;
import com.tzy.common.entity.Dishes;
import com.tzy.dishes.service.DishesService;
import com.tzy.dishes.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜品详细表 前端控制器
 * </p>
 *
 * @author 谭同学
 * @since 2022-03-01
 */
@Slf4j
@RestController
@RequestMapping("/dishes")
public class DishesController extends BaseController {
    @Resource
    private DishesService dishesService;
    @Resource
    private RedisService redisService;

    @GetMapping("/page/fuzzy/query/dishesName")
    public WebResult page( String dishesName,
                           @RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "10") Integer size ){

        Page<Dishes> page = dishesService.getFuzzyQueryDishesPageByDishesName(dishesName, pageNum, size);
        return new WebResult(WebResultMsgCode.SUCCESS,page);
    }

    @GetMapping("/page/{typeId}")
    public WebResult page( @PathVariable int typeId,
                           @RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "10") Integer size ){
        Page<Dishes> page = dishesService.getListByTypeIdPage(typeId,pageNum, size);
        return new WebResult(WebResultMsgCode.SUCCESS,page);
    }
    @GetMapping("/details/{dishesId}")
    public WebResult getDishesDetails( @PathVariable int dishesId ){
        Dishes dishes = dishesService.getDishesDetailsLimit1(dishesId);
        return new WebResult(WebResultMsgCode.SUCCESS,dishes);
    }
    @GetMapping("/list/{shopId}")
    public WebResult getDishesListByShopId(@PathVariable int shopId ){
        List<Dishes> list = dishesService.getDishesListByShopId(shopId);
        return new WebResult(WebResultMsgCode.SUCCESS,list);
    }
    @PostMapping("/insert")
    public WebResult save(String dishesName, int shopId, int dishesTypeId,
                          String dishesPhotoOrVideoUrl,
                          String title, String mainIngredient, String shopkeeperDescription,
                          String taste, BigDecimal price ){
        log.info("dishesTypeId="+dishesTypeId);
        Dishes dishes = new Dishes();
        dishes.setDishesName(dishesName);
        dishes.setShopId(shopId);
        dishes.setDishesTypeId(dishesTypeId);
        dishes.setDishesPhotoOrVideoUrl(dishesPhotoOrVideoUrl);
        dishes.setTitle(title);
        dishes.setShopkeeperDescription(shopkeeperDescription);
        dishes.setMainIngredient(mainIngredient);
        dishes.setTaste(taste);
        dishes.setPrice(price);
        boolean save = dishesService.save(dishes);
        return new WebResult(WebResultMsgCode.SUCCESS,save);
    }
    @PutMapping("/update")
    public WebResult updateById(int id,String dishesName, int shopId, int dishesTypeId,
                          String dishesPhotoOrVideoUrl,
                          String title, String mainIngredient, String shopkeeperDescription,
                          String taste, BigDecimal price ){
        log.info("dishesTypeId="+dishesTypeId);
        Dishes dishes = new Dishes();
        dishes.setId(id);
        dishes.setDishesName(dishesName);
        dishes.setShopId(shopId);
        dishes.setDishesTypeId(dishesTypeId);
        dishes.setDishesPhotoOrVideoUrl(dishesPhotoOrVideoUrl);
        dishes.setTitle(title);
        dishes.setShopkeeperDescription(shopkeeperDescription);
        dishes.setMainIngredient(mainIngredient);
        dishes.setTaste(taste);
        dishes.setPrice(price);
        boolean updateById = dishesService.updateById(dishes);
        return new WebResult(WebResultMsgCode.SUCCESS,updateById);
    }
    /**
     * 修改状态
     * @param id
     * @return
     */
    @PutMapping("/update/status")
    public WebResult updateStatus(int id){
        log.info("id="+ id);
        Dishes dishes = new Dishes();
        dishes.setId(id);
        dishes.setIsDeleted(0);
        boolean updateById = dishesService.updateById(dishes);
        return new WebResult(WebResultMsgCode.SUCCESS,updateById);
    }
    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public WebResult logicalDeletion(int id){
        log.info("id="+ id);
        Dishes dishes = new Dishes();
        dishes.setId(id);
        dishes.setIsDeleted(1);
        boolean updateById = dishesService.updateById(dishes);
        return new WebResult(WebResultMsgCode.SUCCESS,updateById);
    }
    @GetMapping("/info")
    public Dishes getInfo(int dishesId){
        log.info("dishesId="+dishesId);
        return dishesService.getById(dishesId);
    }
    @GetMapping("/list")
    public List<Dishes> list(){
        QueryWrapper<Dishes> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isDeleted",0);
        List<Dishes> list = dishesService.list(queryWrapper);
        return list;
    }

    /**
     * 调用接口让过滤器过滤
     * @return
     */
    @GetMapping("/test")
    public Result test(){
        return Result.success();
    }
    //    @GetMapping("/page/fuzzy/query/{dishesName}")
//    public WebResult page( @PathVariable String dishesName,
//                           @RequestParam(defaultValue = "1") Integer pageNum,
//                           @RequestParam(defaultValue = "10") Integer size ){
//
//        Page<Map<String,Object>> page = dishesService.getAllDishesListMapsPageByDishesName(dishesName, pageNum, size);
//        return new WebResult(WebResultMsgCode.SUCCESS,page);
//    }
//    @Resource
//    private DishesMapper dishesMapper;
//    @GetMapping("/page/{dishesTypeId}/map")
//    public WebResult pageTest(@RequestParam(defaultValue = "1") Integer pageNum,@PathVariable int dishesTypeId){
//        List<Map<String, Object>> mapList = dishesMapper.findAllMapsPageByTypeId(dishesTypeId,pageNum, 5);
//        return new WebResult(WebResultMsgCode.SUCCESS,mapList);
//    }
}
